package com.yksj.healthtalk.ui.messageleave;

import org.apache.commons.lang.math.NumberRange;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONException;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 医生公告板设置
 * @author apple
 *
 */
public class DoctorMessageLeaveSetting extends BaseFragmentActivity implements OnClickListener, OnCheckedChangeListener {

	private ToggleButton toggleButton;
	private EditText messageNumber;//留言次数
	private TextView messageNumberText;//允许次数(1-20次/人/天)
	private int messageStartCount;
	private int messageEndCount;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_message_leave_setting_layout);
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("发布板设置");
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setOnClickListener(this);
		toggleButton = (ToggleButton) findViewById(R.id.distraction_free_zone);
		toggleButton.setOnCheckedChangeListener(this);
		messageNumber = (EditText) findViewById(R.id.message_number);
		messageNumberText = (TextView) findViewById(R.id.message_number_count);//允许次数(1-20次/人/天)
		initData();
		
	}

	private void initData() {
		try {
			//{"START":"1","COUNT":"0","END":"20","code":"0","FLAG":"0"}  
			String object=getIntent().getStringExtra("content");
			org.json.JSONObject jsonObject=new org.json.JSONObject(object);
			messageStartCount = jsonObject.getInt("START");
			messageEndCount = jsonObject.getInt("END");
			messageNumberText.setText("允许次数("+messageStartCount+"-"+messageEndCount+"次/人/天)");
			messageNumber.setText(jsonObject.getString("COUNT"));
			toggleButton.setChecked("1".equals(jsonObject.getString("FLAG")));
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			String str=messageNumber.getText().toString();
			NumberRange range=new NumberRange(messageStartCount, messageEndCount);
			if(toggleButton.isChecked()){
				if(!NumberUtils.isNumber(str) || !range.containsFloat(NumberUtils.toFloat(str))){
				ToastUtil.showLong(this,"允许次数("+messageStartCount+"-"+messageEndCount+"次/人/天)");
				return;
				}else{
					queryData("1", String.valueOf(str));
				}
			}else{
				
				queryData("0", str);
				
			}
			break;
		}
		
	}

	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		if(arg1){
			findViewById(R.id.message_count_line).setVisibility(View.VISIBLE);
		}else{
			findViewById(R.id.message_count_line).setVisibility(View.GONE);
		}
	}
	/**
	 * 设置留言设置
http://192.168.16.118:8085/sms_web/DoctorMessageBoardsServlet?OPTION=UPDATESETINFO&CUSTOMERID=4200&MESSAGEONOFF=0&MESSAGECOUNT=1
	 */
	public void queryData(String messageOff,String messageCount){
		RequestParams params=new RequestParams();
		params.put("OPTION", "UPDATESETINFO");
		params.put("CUSTOMERID", SmartFoxClient.getLoginUserId());
		params.put("MESSAGEONOFF", messageOff);
		params.put("MESSAGECOUNT", messageCount);
		HttpRestClient.doHttpRequestMessage(params, new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(JSONObject response) {
				super.onSuccess(response);
				//成功：{"info":"设置成功","code":"0"}  失败：{"info":"设置失败","code":"2"} 错误{"info":"操作错误","code":"500"}
				if(response.getString("code").equals("500")||response.getString("code").equals("2")){
					ToastUtil.showShort(response.getString("info"));
				}else{
					finish();
					ToastUtil.showShort(response.getString("info"));
				}
			}
			
		});
		
		
		
	}
	
}
